﻿using System.Collections.Generic;
using System.Linq;
using SIG.Repository;
using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Configs;
using SIG.Resources;
using SIG.Model.Store;

namespace SIG.Service
{
    public class CartService :  EntityService<Cart>, ICartService
    {
       
        private readonly ICartRepository _cartRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public CartService(ICartRepository cartRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(cartRepository, cacheService, loggingService)
        {           
            _cartRepository = cartRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }

        public Cart SingleOrDefault(string ShoppingCartId,int goodsId)
        {
           return  _cartRepository.GetFirstOrDefault(c => c.CartId == ShoppingCartId && c.GoodsId == goodsId);
        }
        public Cart Single(string ShoppingCartId, int Id)
        {
            return _cartRepository.GetFirstOrDefault(c => c.CartId == ShoppingCartId && c.Id == Id);
           // _cartRepository.UnitOfWork.Commit();
        }

        public void MigrateCart(string ShoppingCartId,string userName)
        {
            var shoppingCart = _cartRepository.GetFilteredElements(
               c => c.CartId == ShoppingCartId);

            foreach (Cart item in shoppingCart)
            {
                item.CartId = userName;
            }
            _cartRepository.UnitOfWork.Commit();
        }

        public void EmptyCart(string ShoppingCartId)
        {
            var cartItems = _cartRepository.GetFilteredElements(cart => cart.CartId == ShoppingCartId);

            foreach (var cartItem in cartItems)
            {
                _cartRepository.Remove(cartItem);
            }
            _cartRepository.UnitOfWork.Commit();
        }


        //public List<Cart> GetPagedElements(int pageIndex, int pageSize, string keyword, out int totalCount)
        //{
        //    if (string.IsNullOrEmpty(keyword))
        //    { 
        //        return GetPagedElements(pageIndex, pageSize, out totalCount);
        //    }

        //    //get list count
        //    var keyCount = $"{EntityNames.Cart}_ListCount__{keyword}";            
        //    if (SettingsManager.Store.EnableCaching)
        //    {
        //        if (_cacheService.IsSet(keyCount))
        //        {
        //            totalCount = (int)_cacheService.Get(keyCount);

        //        }
        //        else
        //        {
        //            totalCount = _cartRepository.GetAllElements().Where(c=>c.Title.Contains(keyword)).Count();
        //            _cacheService.Set(keyCount, totalCount, SettingsManager.Store.CacheDuration);
        //        }
        //    }
        //    else
        //    {
        //        totalCount = _cartRepository.GetAllElements().Where(c => c.Title.Contains(keyword)).Count();
        //    }


        //    //get list
        //    var key = $"{EntityNames.Cart}_List_{pageIndex}_{pageSize}_{keyword}";

        //    List<Cart> categories;
        //    if (SettingsManager.Store.EnableCaching)
        //    {
        //        if (_cacheService.IsSet(key))
        //        {
        //            categories = (List<Cart>)_cacheService.Get(key);

        //        }
        //        else
        //        {
        //            categories = _cartRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), (c => c.Title.Contains(keyword)), false).ToList();
        //            _cacheService.Set(key, categories, SettingsManager.Store.CacheDuration);
        //        }
        //    }
        //    else
        //    {
        //        categories = _cartRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), (c => c.Title.Contains(keyword)), false).ToList();
        //    }

        //    return categories;            

        //}

        //public List<Cart> GetPagedElements(int pageIndex, int pageSize, out int totalCount)
        //{

        //    //get list count
        //    var keyCount = $"{EntityNames.Cart}_ListCount";
        //    if (SettingsManager.Store.EnableCaching)
        //    {
        //        if (_cacheService.IsSet(keyCount))
        //        {
        //            totalCount = (int)_cacheService.Get(keyCount);

        //        }
        //        else
        //        {
        //            totalCount = _cartRepository.GetAllElements().Count();
        //            _cacheService.Set(keyCount, totalCount, SettingsManager.Store.CacheDuration);
        //        }
        //    }
        //    else
        //    {
        //        totalCount = _cartRepository.GetAllElements().Count();
        //    }


        //    //get list
        //    var key = $"{EntityNames.Cart}_List_{pageIndex}_{pageSize}";

        //    List<Cart> categories;
        //    if (SettingsManager.Store.EnableCaching)
        //    {
        //        if (_cacheService.IsSet(key))
        //        {
        //            categories = (List<Cart>)_cacheService.Get(key);

        //        }
        //        else
        //        {
        //            categories = _cartRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), false).ToList();
        //            _cacheService.Set(key, categories, SettingsManager.Store.CacheDuration);
        //        }
        //    }
        //    else
        //    {
        //        categories = _cartRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), false).ToList();
        //    }

        //    return categories;

        //}
    }
}
